#ifndef _THREAD_COUNTER_HPP_
#define _THREAD_COUNTER_HPP_

#include <pthread.h>

class ThreadCounter
{
    static int _thread_num;
    static pthread_spinlock_t _lock;


public:
    static void Init()
    {
        pthread_spin_init(&_lock, 0);
        _thread_num = 0;
    }
    
    static void BeginThread()
    {
        pthread_spin_lock(&_lock);
        _thread_num++;
        pthread_spin_unlock(&_lock);
    }

    static void EndThread()
    {
        pthread_spin_lock(&_lock);
        _thread_num--;
        pthread_spin_unlock(&_lock);
    }

    static int ThreadNum()
    {
        return _thread_num;
    }
};


int ThreadCounter::_thread_num;
pthread_spinlock_t ThreadCounter::_lock;

#endif

